/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.wf.controller;
import com.wf.common.util.StringUtil;
import com.wf.entity.PageInfo;
import com.wf.entity.User;
import com.wf.model.Result;
import com.wf.provider.UserProvider;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");
        // 默认时间格式 具体在自己改
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
    @Autowired
    private UserProvider userProvider;

    /**
     * 当前所在模块主页
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(
            HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currentPath", request.getContextPath()+ "/user");
        map.put("fieldLabel", getLabels());
        return new ModelAndView("/user/index", map);
    }
    /**
     * 列表页面
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(User user,
                                    HttpServletRequest request,
                                    @RequestParam(required = false, defaultValue = "1") int page,
                                    @RequestParam(required = false, defaultValue = "20") int rows) {
        String name=request.getParameter("name");
        user.setPage(page);
        user.setRows(rows);
        PageInfo<User> pageInfo = userProvider.getAllByPage(user);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        result.put("fieldLabel", getLabels());
        return result;
    }
    /**
     * 编辑页面
     */
    @RequestMapping(value = "/edit/{id}")
    public ModelAndView edit(@PathVariable("id") String id,
                             HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        User emp = null;
        if (!StringUtil.empty(id) && !id.equals("-1")) {
            emp = userProvider.get(id);
        }
        map.put("emp", emp);
        map.put("currentPath", request.getContextPath()+ "/user");
        map.put("fieldLabel", getLabels());
        return new ModelAndView("/user/edit", map);
    }
    /**
     * 详情页面
     */
    @RequestMapping(value = "/detail/{id}")
    public ModelAndView detail(@PathVariable("id") String id) {
        User emp = userProvider.get(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("emp", emp);
        map.put("fieldLabel", getLabels());
        return new ModelAndView("/user/detail", map);
    }
    /**
     * 保存方法
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<Integer> save(@ModelAttribute("emp") User emp) {
        Result<Integer> result = new Result<Integer>();
        return result;
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result<Integer> remove(@RequestParam("id") String[] id) {
        Result<Integer> result = new Result<Integer>();
        for (int i = 0; i < id.length; i++) {
            userProvider.delete(id[i]);
        }
        result.setContent(1);
        return result;
    }
    /**
     * 表字段中文显示
     */
    public Map<String, String> getLabels() {
        // key field name,value:field label
        java.util.Map<String, String> map = new java.util.HashMap<String, String>();
        map.put("id","id");
        map.put("enterpriseId","所属集团");
        map.put("deptId","所属部门");
        map.put("deptName","部门名称");
        map.put("name","姓名");
        map.put("phone","电话");
        map.put("position","职位");
        map.put("sex","性别");
        map.put("loginName","登陆名");
        map.put("createUser","创建人");
        map.put("createTime","创建时间");
        map.put("lastModifyUser","最后修改人");
        map.put("lastModifyTime","最后修改时间");
        map.put("delFlag","是否删除");
        return map;
    }

}
